<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
    <style>
        .layui-form-item .layui-input-inline {
            width: 150px;
        }

        .layui-form-label {
            padding: 9px 5px;
            width: auto;
        }

        .btn_danger {
            background-color: #FFB800;
        }
    </style>
</head>

<body>

    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <form name="formName" onsubmit="return false">
                    <div class="layui-form-item">
                        <input type="hidden" name="source" value="{$source}" id="sourceId">
                        <div class="layui-inline">
                            <label class="layui-form-label">设备id</label>
                            <div class="layui-input-inline">
                                <input type="text" value="" name="device_id" placeholder="请输入" autocomplete="off"
                                    class="layui-input" />
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">下载时间</label>
                            <div class="layui-input-inline" style="width: 350px;">
                                <input type="text" value="" name="start_time" placeholder="搜索开始时间" autocomplete="off"
                                    class="layui-input" style="width: 167px; display: inline" id="start_time_input" />
                                -
                                <input type="text" value="" name="end_time" placeholder="搜索截止日期" autocomplete="off"
                                    class="layui-input" style="width: 167px; display: inline" id="end_time_input" />
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-list" lay-submit
                                lay-filter="LAY-appdownload-front-search">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="LAY-appdownload-manage" lay-filter="LAY-appdownload-manage"></table>
            <script type="text/html" id="table-appdownload-operation">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="reset">
                    <i class="layui-icon"></i>清零观看时长
                </a>
            </script>
        </div>
    </div>

    <script src="/layuiadmin/layui/layui.js"></script>
    <script>
        layui.config({
            base: '/layuiadmin/'
            , version: "{:config('other.ui_version')}"
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'table', 'admin', 'laydate'], function () {
            var $ = layui.$
                , form = layui.form
                , table = layui.table
                , admin = layui.admin
                , laydate = layui.laydate;


            //执行一个laydate实例
            var start_time_2 = laydate.render({
                elem: '#start_time_input',
                type: 'datetime'
            });
            var end_time_2 = laydate.render({
                elem: '#end_time_input',
                type: 'datetime',
            });


            // ip统计
            table.render({
                elem: '#LAY-appdownload-manage'
                , url: '{:url("appdownload/getlist")}?source=' + $('#sourceId').val()
                , cols: [[
                    { field: 'id', title: 'ID', width: 100 }
                    , { field: 'device_id', title: '设备id', width: 500 }
                    , { field: 'source', title: '渠道标示', width: 500 }
                    , { field: 'time', title: '下载时间', width: 300 }
                    , { field: 'source', title: '直播观看时间', width: 300, templet: function(d) {
                        return d.duration? Math.floor(d.duration / 3600) + '时' + Math.floor((d.duration % 3600) / 60) + '分' + (d.duration % 60) + '秒' : '';
                    }},
                    , { title: '操作', width: 600, align: 'center',  toolbar: '#table-appdownload-operation' }
                ]]
                , page: true
                , limit: 20
                , height: 'full-140'
                , text: '对不起，加载出现异常！'
                , even: true
            });

            //监听搜索
            form.on('submit(LAY-appdownload-front-search)', function (data) {
                var field = data.field;
                if (field.end_time && field.start_time && field.end_time < field.start_time) {
                    return layer.msg('结束时间必须大于开始时间', { icon: 5 });
                }

                //执行重载
                table.reload('LAY-appdownload-manage', {
                    where: field
                    , page: {
                        curr: 1   //默认从第一页开始搜索
                    },
                });
            });
            
            //监听工具条
            table.on('tool(LAY-appdownload-manage)', function (obj) {
                if (obj.event === 'reset') {
                    console.log(obj);
                    layer.confirm('确定要清空该设备id观看时长吗？', function (index) {
                        admin.req({
                            url: '/admin/appdownload/reset'
                            , data: {"device_id": obj.data.device_id}
                            , success(res) {
                                if (res.code == 0) {
                                    table.reload("LAY-appdownload-manage");
                                    layer.msg(res.msg, {icon: 1});
                                } else {
                                    layer.msg(res.msg, {icon: 5});
                                }
                            }
                        });
                    });
                }
            });
        });
    </script>
</body>

</html>